package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.interactive.UserInteractive;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserInteractiveDaoImpl extends TempDataObjectDao<UserInteractive> {

    public UserInteractive getUserInteractive(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_interacitve where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserInteractive data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean addData(UserInteractive data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_interacitve(userId,modifyTime,likeUserIdList,hateUserIdList,totalGiveLikeTimes) values (?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getModifyTime());
            call.setString(i++, data.getLikeUserIdListString());
            call.setString(i++, data.getHateUserIdListString());
            call.setLong(i++, data.getTotalGiveLikeTimes());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserInteractive data) {
        PreparedStatement call = null;
        String sql = "update t_u_interacitve set modifyTime=?,likeUserIdList=?,hateUserIdList=?,totalGiveLikeTimes=? where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getModifyTime());
            call.setString(i++, data.getLikeUserIdListString());
            call.setString(i++, data.getHateUserIdListString());
            call.setLong(i++, data.getTotalGiveLikeTimes());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserInteractive parseData(ResultSet rs) throws SQLException {
        UserInteractive data = new UserInteractive();
        data.setUserId(rs.getLong("userId"));
        data.setModifyTime(rs.getLong("modifyTime"));
        data.setLikeUserIdList(StringUtils.stringToLongList(rs.getString("likeUserIdList"), ","));
        data.setHateUserIdList(StringUtils.stringToLongList(rs.getString("hateUserIdList"), ","));
        data.setTotalGiveLikeTimes(rs.getLong("totalGiveLikeTimes"));
        return data;
    }
}
